![]() |
PATH![]() |
![]() ![]() |
If statements allow you to define statements or groups of statements that are executed only in specific circumstances. Each If statement contains one or more Boolean expressions whose values can be either true or false . AppleScript executes the statements contained in the If statement only if the value of the Boolean expression is true .
If statements are also called conditional statements. Boolean expressions in If statements are also called tests.
The following example uses an If statement to determine whether to display a dialog box.
if dependents > 2 then
display dialog "You might need to file an extra form"
end if
The If statement contains the Boolean expression dependents > 2 . If the value of the Boolean expression is true , the Display Dialog command is executed. If the value of the Boolean expression is false , the Display Dialog command is not executed.
Display Dialog is one of the standard scripting addition commands distributed with AppleScript. For information about these commands, see the AppleScript section of the Mac OS Help Center, or see the AppleScript website at
If statements can contain multiple tests. For example, the following statement contains two tests to determine if two files exist before using them.
tell application "Finder"
if (the file "Hard Disk:Status Report" exists) then
if (the file "Hard Disk:Department Status" exists) then
tell application "AppleWorks"
-- Statements that open the files, perform operations
-- on them, and close the files. (Not shown.)
end tell -- AppleWorks
end if -- second file exists
end if -- first file exists
end tell -- tell Finder
The two If statements in the previous example can be combined into one If statement with two tests:
tell application "Finder"
if (the file "Hard Disk:Status Report" exists) and ¬
(the file "Hard Disk:Department Status" exists) then
-- Other statements not shown.
end if -- files exist
end tell -- tell Finder
Rather than test whether a file exists before performing operations on it, you can use a Try statement that always attempts to operate on the file, but contains an error branch to deal with the case where the file doesn't exist. Try statements are described in Try Statements.
An If statement can contain any number of Else If clauses; AppleScript looks for the first Boolean expression contained in an If or Else If clause that is true , executes the statements contained in its block (the statements between one Else If and the following Else If or Else clause), and then exits the If statement.
An If statement can also include a final Else clause. The statements in its block are executed if no other test in the If statement passes. Consider the following, more elaborate If statement.
display dialog "How many dependents?" default answer ""
set dependents to (text returned of result) as integer
--Could test here to be sure user entered a valid number.
display dialog "Have you ever been audited?" buttons {"No", "Yes"}
if button returned of result = "Yes" then
set audit to true
else
set audit to false
end if
if dependents < 9 and audit = false then
display dialog "No extra forms are required."
else if dependents < 9 and audit = true then
display dialog "You might need to file an extra form."
else --anything greater than 9
display dialog "You will need to file an extra form."
end if
This example uses Boolean operators to create a more complex Boolean expression. For example, the expression
dependents < 9 and audit = false
uses the Boolean operator And to combine two Boolean operands ( dependents < 9 and audit = false ). If both expressions are true , the value of the entire expression is true . You can also use the Boolean operator Or (another binary operator; if either of its operands is true , the entire expression is true ) and the Boolean operator Not (a unary operator; if its operand is true , the expression is false , and vice versa). For more information about operators, see Expressions.